<template>
  <el-dialog
    :title="!dataForm.goodId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商品名称" prop="goodName">
      <el-input v-model="dataForm.goodName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="分类id" prop="categoryId">
      <el-input v-model="dataForm.categoryId" placeholder="分类id"></el-input>
    </el-form-item>
    <el-form-item label="商品编码" prop="goodNumber">
      <el-input v-model="dataForm.goodNumber" placeholder="商品编码"></el-input>
    </el-form-item>
    <el-form-item label="商品照片" prop="goodImg">
      <el-input v-model="dataForm.goodImg" placeholder="商品照片"></el-input>
    </el-form-item>
    <el-form-item label="商品价格" prop="goodPrice">
      <el-input v-model="dataForm.goodPrice" placeholder="商品价格"></el-input>
    </el-form-item>
    <el-form-item label="商品介绍" prop="goodDescribe">
      <el-input v-model="dataForm.goodDescribe" placeholder="商品介绍"></el-input>
    </el-form-item>
    <el-form-item label="售后服务" prop="goodServer">
      <el-input v-model="dataForm.goodServer" placeholder="售后服务"></el-input>
    </el-form-item>
    <el-form-item label="是否秒杀" prop="seckillIs">
      <el-input v-model="dataForm.seckillIs" placeholder="是否秒杀"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="goodCreatetime">
      <el-input v-model="dataForm.goodCreatetime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="库存" prop="goodSum">
      <el-input v-model="dataForm.goodSum" placeholder="库存"></el-input>
    </el-form-item>
    <el-form-item label="秒杀id" prop="seckillId">
      <el-input v-model="dataForm.seckillId" placeholder="秒杀id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          goodId: 0,
          goodName: '',
          categoryId: '',
          goodNumber: '',
          goodImg: '',
          goodPrice: '',
          goodDescribe: '',
          goodServer: '',
          seckillIs: '',
          goodCreatetime: '',
          goodSum: '',
          seckillId: ''
        },
        dataRule: {
          goodName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          categoryId: [
            { required: true, message: '分类id不能为空', trigger: 'blur' }
          ],
          goodNumber: [
            { required: true, message: '商品编码不能为空', trigger: 'blur' }
          ],
          goodImg: [
            { required: true, message: '商品照片不能为空', trigger: 'blur' }
          ],
          goodPrice: [
            { required: true, message: '商品价格不能为空', trigger: 'blur' }
          ],
          goodDescribe: [
            { required: true, message: '商品介绍不能为空', trigger: 'blur' }
          ],
          goodServer: [
            { required: true, message: '售后服务不能为空', trigger: 'blur' }
          ],
          seckillIs: [
            { required: true, message: '是否秒杀不能为空', trigger: 'blur' }
          ],
          goodCreatetime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          goodSum: [
            { required: true, message: '库存不能为空', trigger: 'blur' }
          ],
          seckillId: [
            { required: true, message: '秒杀id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.goodId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.goodId) {
            this.$http({
              url: this.$http.adornUrl(`/goods/product/info/${this.dataForm.goodId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.goodName = data.product.goodName
                this.dataForm.categoryId = data.product.categoryId
                this.dataForm.goodNumber = data.product.goodNumber
                this.dataForm.goodImg = data.product.goodImg
                this.dataForm.goodPrice = data.product.goodPrice
                this.dataForm.goodDescribe = data.product.goodDescribe
                this.dataForm.goodServer = data.product.goodServer
                this.dataForm.seckillIs = data.product.seckillIs
                this.dataForm.goodCreatetime = data.product.goodCreatetime
                this.dataForm.goodSum = data.product.goodSum
                this.dataForm.seckillId = data.product.seckillId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/goods/product/${!this.dataForm.goodId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'goodId': this.dataForm.goodId || undefined,
                'goodName': this.dataForm.goodName,
                'categoryId': this.dataForm.categoryId,
                'goodNumber': this.dataForm.goodNumber,
                'goodImg': this.dataForm.goodImg,
                'goodPrice': this.dataForm.goodPrice,
                'goodDescribe': this.dataForm.goodDescribe,
                'goodServer': this.dataForm.goodServer,
                'seckillIs': this.dataForm.seckillIs,
                'goodCreatetime': this.dataForm.goodCreatetime,
                'goodSum': this.dataForm.goodSum,
                'seckillId': this.dataForm.seckillId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
